package cn.tedu.sp03userservice.controller;

import cn.tedu.entity.User;
import cn.tedu.service.UserService;
import cn.tedu.util.JsonResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;

@RestController
@Slf4j
public class UserController {
    @Autowired
    private UserService userService;

    //获取用户
    //http://localhost:8101/5
    @GetMapping("/{userId}")
    public JsonResult<User> getUser(@PathVariable Integer userId){

        log.warn("userId为"+userId);

        User user = userService.getUser(userId);
        return JsonResult.build().code(200).data(user);
    }

    //增加积分
    //方法路径： http://localhost:8101/8/1000            在方法参数名和路径参数名一致的情况下，可以省略
    //方法路径： http://localhost:8101/8/score?score=1000    语义更加清晰一些
    @GetMapping("/{userId}/score")  //加了大括号，是路径参数
    public  JsonResult<?>addScore(@PathVariable Integer userId,Integer score){

        log.warn("userId为"+userId+"score为"+score);

        userService.addScore(userId, score);
        return JsonResult.build().code(200).msg("增加积分成功");
    }

    @GetMapping("/favicon.ico")
    public void ico(){
    }
}
